package com.ssm.comm.session;


import com.ssm.comm.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 分布式Session 过滤器
 */
public class ShareSessionFilter implements Filter {

    private final static String SESSION_TOKEN = "SESSION_ID";
    private final static Set<String> B_URL = new HashSet<>() ;
    private static  ShareSessionService share ;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO 白名单 B_URL
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest ;
        HttpServletResponse response = (HttpServletResponse)servletResponse ;
        if(Objects.isNull(share)){
            share = SpringContextUtil.getBean(ShareSessionService.class) ;
        }

        String uri = request.getPathInfo() ; // 如果为Null 应该是 JSP或者 JS什么的
//        if(StringUtils.isBlank(uri) || B_URL.contains(uri) ||
//                !request.getRequestURI().startsWith("/FIVE-SSM-TOMCAT/test")){
//            filterChain.doFilter(request,response); // 原生态无需分布式Session
//            /FIVE-SSM-TOMCAT/test这种需要进行分布式session
//            return;
//        }else{
//            String token = request.getHeader(SESSION_TOKEN) ;
//            if(StringUtils.isBlank(token) || !share.existSession(token)){
//                err();return;
//            }
//            if(!(request instanceof MyHttpServletRequestWrapper)){
//                // 替换一下 请求
//                request = new MyHttpServletRequestWrapper(token,request,response);
//                // 覆盖 Spring的Threadlocal
//                RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request),true);
//            }
//        }
//        filterChain.doFilter(request,response);

        if(StringUtils.isNotBlank(uri) && !B_URL.contains(uri) &&
                request.getRequestURI().startsWith("/FIVE-SSM-TOMCAT/test") ){
            String token = request.getHeader(SESSION_TOKEN) ;
            if(StringUtils.isBlank(token) || !share.existSession(token)){
                err();return;
            }
            if(!(request instanceof MyHttpServletRequestWrapper)){
                // 替换一下 请求
                request = new MyHttpServletRequestWrapper(token,request,response);
                // 覆盖 Spring的Threadlocal
                RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request),true);
            }
        }
        filterChain.doFilter(request,response);
    }

    private void err()throws  ServletException{
        throw new ServletException("获取session失败！！！！");
    }



    @Override
    public void destroy() {

    }
}
